package com.haoxuer.discover.area.rest.resource;

import com.haoxuer.discover.area.data.entity.Area;
import com.haoxuer.discover.area.rest.conver.AreaDataSimpleConver;
import com.haoxuer.discover.area.rest.api.AreaHandler;
import com.haoxuer.discover.area.data.dao.AreaDao;
import com.haoxuer.discover.area.rest.domain.request.AreaCodeRequest;
import com.haoxuer.discover.area.rest.domain.request.AreaLevelRequest;
import com.haoxuer.discover.area.rest.domain.request.AreaListRequest;
import com.haoxuer.discover.area.rest.domain.request.AreaTypeRequest;
import com.haoxuer.discover.area.rest.domain.response.list.AreaDataList;
import com.haoxuer.discover.area.rest.domain.response.list.AreaList;
import com.haoxuer.discover.area.rest.domain.response.simple.AreaDataSimple;
import com.haoxuer.discover.area.rest.domain.response.simple.AreaSimple;
import com.haoxuer.discover.area.rest.conver.AreaSimpleConver;
import com.haoxuer.discover.data.page.Filter;
import com.haoxuer.discover.data.page.Order;
import java.util.ArrayList;
import java.util.List;

import com.haoxuer.discover.user.utils.ConverResourceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Scope("prototype")
@Transactional
@Component
public class AreaResource implements AreaHandler {
  
  @Autowired
  AreaDao areaDao;
  
  @Override
  public AreaList list(AreaListRequest request) {
    
    AreaList result = new AreaList();
    List<Order> orders = new ArrayList<Order>();
    orders.add(Order.asc("lft"));
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(Filter.eq("parent.id", request.getId()));
    List<Area> areas = areaDao.list(0, request.getSize(), filters, orders);
    List<AreaSimple> simples = ConverResourceUtils.coverList(areas, new AreaSimpleConver());
    result.setList(simples);
    
    return result;
  }
  
  @Override
  public AreaList list(AreaCodeRequest request) {
    AreaList result = new AreaList();
    List<Order> orders = new ArrayList<Order>();
    orders.add(Order.asc("lft"));
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(Filter.eq("parent.code", request.getCode()));
    List<Area> areas = areaDao.list(0, request.getSize(), filters, orders);
    List<AreaSimple> simples = ConverResourceUtils.coverList(areas, new AreaSimpleConver());
    result.setList(simples);
    
    return result;
  }
  
  @Override
  public AreaList level(AreaLevelRequest request) {
    AreaList result = new AreaList();
    List<Order> orders = new ArrayList<Order>();
    orders.add(Order.asc("lft"));
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(Filter.eq("levelInfo", request.getLevel()));
    List<Area> areas = areaDao.list(0, request.getSize(), filters, orders);
    List<AreaSimple> simples = ConverResourceUtils.coverList(areas, new AreaSimpleConver());
    result.setList(simples);
    
    return result;
  }
  
  @Override
  public AreaDataList id(AreaListRequest request) {
    AreaDataList result = new AreaDataList();
    List<Order> orders = new ArrayList<Order>();
    orders.add(Order.asc("id"));
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(Filter.eq("parent.id", request.getId()));
    List<Area> areas = areaDao.list(0, request.getSize(), filters, orders);
    List<AreaDataSimple> simples = ConverResourceUtils.coverList(areas, new AreaDataSimpleConver());
    result.setList(simples);
    
    return result;
  }
  
  @Override
  public AreaDataList code(AreaCodeRequest request) {
    AreaDataList result = new AreaDataList();
    List<Order> orders = new ArrayList<Order>();
    orders.add(Order.asc("lft"));
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(Filter.eq("parent.code", request.getCode()));
    List<Area> areas = areaDao.list(0, request.getSize(), filters, orders);
    List<AreaDataSimple> simples = ConverResourceUtils.coverList(areas, new AreaDataSimpleConver());
    result.setList(simples);
    
    return result;
  }
  
  @Override
  public AreaList type(AreaTypeRequest request) {
    AreaList areaList = new AreaList();
    List<Order> orders = new ArrayList<Order>();
    orders.add(Order.asc("id"));
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(Filter.eq("type", request.getType()));
    List<Area> areas = areaDao.list(0, 10000, filters, orders);
    List<AreaSimple> simples = ConverResourceUtils.coverList(areas, new AreaSimpleConver());
    areaList.setList(simples);
    
    return areaList;
  }
}
